package com.wanggsx.library.base.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

/**
 * Created by wanggsx on 15/12/9.
 */
public class HorizontalPagerScroller0 extends HorizontalScrollView {

    private Context mContext;
    public LinearLayout mContainer;

    private int MOVE_SMALL_DISTANCE = 40;

    public HorizontalPagerScroller0(Context context) {
        super(context);
    }

    public HorizontalPagerScroller0(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mContainer = new LinearLayout(context, attrs);
        addView(mContainer);
        setHorizontalScrollBarEnabled(false);
    }

    /**
     * 初始化容器的尺寸及位置
     *
     * @param width        宽度
     * @param height       高度
     * @param marginLeft   左边距
     * @param marginTop    上边距
     * @param marginRight  右边距
     * @param marginBottom 底边距
     */
    public void setLocation(int width, int height, int marginLeft, int marginTop, int marginRight, int marginBottom) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
        params.setMargins(marginLeft, marginTop, marginRight, marginBottom);
        setLayoutParams(params);
    }

    /**
     * 设置页数
     *
     * @param num         总页数
     * @param layoutId    布局文件id
     * @param width       页宽
     * @param height      页高
     * @param marginLeft  页的左外边距(最后一个页的右外边距同左，其余右边距为0)
     * @param paddingLeft 容器的左内边距(右内边距同左)
     */
    public void setPageNumAndSize(int num, int layoutId, int width, int height, int marginLeft, int paddingLeft) {
        mContainer.setPadding(paddingLeft, 0, paddingLeft, 0);
        if (num >= 1) {
            totalPage = num;
            currentPageIndex = 0;
            originX = 0;
            pagerW = width + marginLeft;
            for (int i = 0; i < num; i++) {
                View child = LayoutInflater.from(mContext).inflate(layoutId, null);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
                params.setMargins(marginLeft, 0, (num == i + 1) ? marginLeft : 0, 0);
                child.setLayoutParams(params);
                mContainer.addView(child);
            }
        }
    }

    public void scrollToFirst() {
        currentPageIndex = 0;
        smoothScrollTo(0, 0);
    }


    private int totalPage, currentPageIndex, nextPageIndex, pagerW;
    private float startMoveX, scrollDistance, originX;
    private boolean tfIsFirstMove = true;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startMoveX = ev.getX();
                tfIsFirstMove = false;
                break;
            case MotionEvent.ACTION_UP:
                scrollDistance = ev.getX() - startMoveX;
                if (scrollDistance > 0) {
                    //向右滑
                    if (scrollDistance > MOVE_SMALL_DISTANCE) {
                        nextPageIndex = currentPageIndex >= 1 ? --currentPageIndex : 0;
                    } else {
                        nextPageIndex = currentPageIndex;
                    }
                } else {
                    //向左滑
                    if (scrollDistance < -MOVE_SMALL_DISTANCE) {
                        nextPageIndex = currentPageIndex == totalPage - 1 ? currentPageIndex : ++currentPageIndex;
                    } else {
                        nextPageIndex = currentPageIndex;
                    }
                }
                originX = pagerW * nextPageIndex;
                smoothScrollTo((int) originX, 0);
                tfIsFirstMove = true;
                break;
            case MotionEvent.ACTION_MOVE:
                if (tfIsFirstMove) {
                    startMoveX = ev.getX();
                    tfIsFirstMove = false;
                }
                float scrollDistance = ev.getX() - startMoveX;
                scrollTo((int) (originX - scrollDistance), 0);
                break;
            case MotionEvent.ACTION_CANCEL:
                originX = pagerW * currentPageIndex;
                smoothScrollTo((int) originX, 0);
                tfIsFirstMove = true;
                break;
        }
        return true;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    }

}
